package com.mac.cxcybackground.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mac.cxcybackground.bean.Depts;
import com.mac.cxcybackground.mapper.StudentsMapper;
import com.mac.cxcybackground.service.DeptsService;
import com.mac.cxcybackground.service.StudentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Slf4j
@Controller
public class DeptsController {
    @Autowired
    DeptsService deptsService;
    @Autowired
    StudentsService studentsService;
    @Autowired
    StudentsMapper studentsMapper;

/**
     * 分页查找
     * @param pn
     * @return
     */

    @GetMapping("/depts_table")
    public String DeptsFind(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                            Model model){
        Page<Depts> deptsPage = new Page<>(pn, 8);
        Page<Depts> page = deptsService.page(deptsPage,null);
        QueryWrapper w1 = new QueryWrapper();
        w1.eq("did",1L);
        Integer count1 = studentsMapper.selectCount(w1);
        model.addAttribute("count1",count1);
        QueryWrapper w2 = new QueryWrapper();
        w2.eq("did",2L);
        Integer count2 = studentsMapper.selectCount(w2);
        model.addAttribute("count2",count2);
        QueryWrapper w3 = new QueryWrapper();
        w3.eq("did",3L);
        Integer count3 = studentsMapper.selectCount(w3);
        model.addAttribute("count3",count3);
        QueryWrapper w4 = new QueryWrapper();
        w4.eq("did",4L);
        Integer count4 = studentsMapper.selectCount(w4);
        model.addAttribute("count4",count4);
        QueryWrapper w5 = new QueryWrapper();
        w5.eq("did",5L);
        Integer count5 = studentsMapper.selectCount(w5);
        model.addAttribute("count5",count5);
        model.addAttribute("page",page);
        return "table/depts_table";
    }
    @GetMapping("/depts_add_table")
    public String depts_add_table(Model model){
      /*  int count = studentsDept01Service.getMap(null).size();*/

        return "table/depts_add_table";
    }
    @PostMapping("/depts/add")
    public String DeptsAdd(Depts depts,Model model){

            Depts dept = new Depts(null,depts.getDName(),depts.getDCount(),depts.getDShow());
            deptsService.save(dept);
        model.addAttribute("success","添加成功");
        return "redirect:/depts_table";
    }
    @GetMapping("/depts/delete/{id}")
    public String DeptsDelete(@PathVariable("id") Long id,
                              @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                              RedirectAttributes ra){
        deptsService.removeById(id);
        ra.addAttribute("pn",pn);
        return "redirect:/depts_table";
    }
    @GetMapping("/depts/update-get/{id}")
    public String DeptsUpdate_get(@PathVariable("id") Long id,
                                  @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                  RedirectAttributes ra,Model model){
        Depts result = deptsService.getById(id);
        ra.addAttribute("pn",pn);
        model.addAttribute("result",result);
        return "table/depts_update_table";
    }
    @GetMapping("/depts/update/{id}")
    public String DeptsUpdate(@PathVariable("id") Long id,Depts depts){
        Depts dept = new Depts(id,depts.getDName(),depts.getDCount(),depts.getDShow());
        deptsService.updateById(dept);
        return "redirect:/depts_table";
    }
}

